<template>
    <div class="swipe-main">
        <transition v-on:before-enter="beforeenter"
                    v-on:enter="enter"
                    v-on:after-enter="afterenter">
            <div class="ball" v-show="ballflag" ref="ball"></div>
        </transition>
<!--        轮播图-->
        <div class="mui-card">
            <div class="mui-card-content">
                <swipe :img="lunbolist" :isfull="false"></swipe>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">{{goodsinfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">

                    市场价：<del>¥{{goodsinfo.market_price}}</del> &nbsp;&nbsp;&nbsp;销售价：<span class="now-price">¥{{goodsinfo.sell_price}}</span>
                    <p>
                        购买数量：<numbox @getnum="getnum" :max="goodsinfo.stock_quantity"></numbox>
                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="bug">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{goodsinfo.goods_no}}</p>
                    <p>库存情况：{{goodsinfo.stock_quantity}} 台</p>
                    <p>上架时间：{{goodsinfo.add_time | timeFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" plain="" size="large" @click="godesc(id)">图文介绍</mt-button>
                <mt-button type="danger" plain="" size="large" class="com" @click="gocomm(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import axios from "axios"
    import swipe from "../subcom/swipe.vue"
    import numbox from "../subcom/numberbox.vue"

    export default {
        name: "goodinfo",
        data(){
            return{
                id : this.$route.params.id,
                lunbolist: [],
                goodsinfo:{},
                ballflag:false,
                num: 1
            }
        },
        methods:{
            getlunbo(){
                axios.get("api/getthumimages/" + this.id)
                    .then(suc=>{
                        // console.log(suc)
                        this.lunbolist = suc.data.message
                    })
                    .catch(err=>{
                        console.log(err)
                    })
            },
            getgoodsinfo(){
                axios.get("api/goods/getinfo/" +this.id)
                    .then(suc=>{
                        // console.log(suc)
                        this.goodsinfo = suc.data.message[0]
                    })
                    .catch(err=>{
                        console.log(err)
                    })
            },
            godesc(id){
                this.$router.push({name: "godesc", params:{id}})
            },
            gocomm(id){
                this.$router.push({name:"gocomm", params:{id}})
            },
            bug(){
                this.ballflag = !this.ballflag
                let good = {id: this.id, count:this.num, price:this.goodsinfo.sell_price, selected: true}

                this.$store.commit("addshopcar", good)
                // console.log(this.good);
            },
            beforeenter(el){
                el.style.transform = "translate(0,0)"
            },
            enter(el, done){
                el.offsetLeft

                const ballposi = this.$refs.ball.getBoundingClientRect();
                const badgeposi = document.querySelector(".badge").getBoundingClientRect()

                let x = badgeposi.left - ballposi.left
                let y = badgeposi.top - ballposi.top

                // console.log(x , y)

                el.style.transform = `translate(${x}px, ${y}px)`
                el.style.transition = "all .5s cubic-bezier(.17,.67,.83,.67)"
                done()
            },
            afterenter(el){
                this.ballflag = !this.ballflag
            },
            getnum(num){
                this.num = num
                // console.log(this.num)
            }
        },
        created(){
            this.getlunbo()
            this.getgoodsinfo()
        },
        components:{
            swipe,
            numbox
        }
    }
</script>

<style scoped lang="less">
    .swipe-main{
        background-color: #ccc;

        .now-price{
            font-size: 16px;
            color: red;
            font-weight: 600;
        }

    }

     .mui-card-footer, .mui-card-header{
         display: block;
     }
    .mint-button--danger.is-plain{
        margin-top: 8px;
    }


        .ball{
            width: 18px;
            height: 18px;
            background-color: red;
            position: absolute;
            left: 40%;
            top:55%;
            z-index: 9999;
            border-radius: 50%;
        }
</style>